package com.itheima.skyserver.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.skyserver.context.BaseContext;
import com.itheima.skyserver.mapper.CategoryMapper;
import com.itheima.skyserver.pojo.domain.Category;
import com.itheima.skyserver.result.PageResult;
import com.itheima.skyserver.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResult page(String name, Integer page, Integer pageSize, Integer type) {
        PageHelper.startPage(page, pageSize);
        Page<Category> categories = categoryMapper.page(name, type);
        return new PageResult(categories.getTotal(), categories.getResult());
    }

    @Override
    public void setStatus(Integer status, Long id) {
        categoryMapper.setStatus(status, id);
    }

    @Override
    public void insertCategory(Category category) {
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setCreateUser(BaseContext.getCurrentId());
        categoryMapper.insertCategory(category);
    }

    @Override
    public void deleteCategoryById(Integer id) {
        categoryMapper.deleteCategoryById(id);
    }

    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }

    @Override
    public void updateCategory(Category category) {
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.updateCategory(category);
    }
}
